package job;

import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 替换 frxsframework-elasticjob-1.0.8.jar，elastic-job启动问题解决
 * @author MingWu.Chen
 * @date 2022年06月13日 20:03
 */

@Configuration
@ConditionalOnProperty(
        prefix = "elastic.job.zk",
        name = {"serverLists", "namespace"},
        matchIfMissing = false
)
@EnableConfigurationProperties({JobZookeeperProperties.class})
public class JobParserAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(JobParserAutoConfiguration.class);

    public JobParserAutoConfiguration() {

    }

    @Bean(
            initMethod = "init"
    )
    public ZookeeperRegistryCenter zookeeperRegistryCenter(JobZookeeperProperties jobZookeeperProperties) {
        ZookeeperConfiguration zkConfig = new ZookeeperConfiguration(jobZookeeperProperties.getServerLists(), jobZookeeperProperties.getNamespace());
        zkConfig.setBaseSleepTimeMilliseconds(jobZookeeperProperties.getBaseSleepTimeMilliseconds());
        zkConfig.setConnectionTimeoutMilliseconds(jobZookeeperProperties.getConnectionTimeoutMilliseconds());
        zkConfig.setDigest(jobZookeeperProperties.getDigest());
        zkConfig.setMaxRetries(jobZookeeperProperties.getMaxRetries());
        zkConfig.setMaxSleepTimeMilliseconds(jobZookeeperProperties.getMaxSleepTimeMilliseconds());
        zkConfig.setSessionTimeoutMilliseconds(jobZookeeperProperties.getSessionTimeoutMilliseconds());
        log.info(String.format("初始化job注册中心配置成功,zkAddress=%s,namespace=%s", zkConfig.getServerLists(), zkConfig.getNamespace()));
        return new ZookeeperRegistryCenter(zkConfig);
    }

    @Bean
    public ElasticJobConfParser elasticJobConfParser() {
        return new ElasticJobConfParser();
    }
}
